package day06;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

// @WebFilter(urlPatterns = "/*")
public class LogFilter implements Filter {

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request= (HttpServletRequest) servletRequest;
        HttpServletResponse response= (HttpServletResponse) servletResponse;


        try {
            System.out.println(request.getRemoteAddr()+"在"+new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(new Date())+"发送"+request.getRequestURI()+"请求");

            chain.doFilter(request,response);

            System.out.println(request.getRemoteAddr()+"在"+new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(new Date())+"执行完"+request.getRequestURI()+"请求");
        } catch (Exception e) {
            System.out.println(request.getRemoteAddr()+"在"+new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(new Date())+"发送"+request.getRequestURI()+"请求出错");
            throw e;
        }
    }

    @Override
    public void destroy() {

    }

    @Override
    public void init(FilterConfig filterConfig) {

    }
}
